import api from '@/api'
import { isCode } from '@/utils'
const initState = () => {
    return {
        //卡片列表
        cardList: [
            {
                tagList: [
                    {
                        name: '#The Great Vowel Movem The Great Vowel Movem',
                        id: 1,
                        count: 26,
                        isClick: false,
                    },
                    {
                        name: '#Philosophy#The Great Vowel Movem The Great Vowel Movem',
                        id: 2,
                        count: 17,
                        isClick: false,
                    },
                    {
                        name: '#Values#The Great Vowel Movem The Great Vowel Movem',
                        id: 3,
                        count: 12,
                        isClick: false,
                    },
                    {
                        name: '#Personal Growth#The Great Vowel Movem The Great Vowel Movem',
                        id: 4,
                        count: 11,
                        isClick: false,
                    },
                    {
                        name: '#Music Notes#The Great Vowel Movem The Great Vowel Movem',
                        id: 5,
                        count: 9,
                        isClick: false,
                    }
                ],
                desc: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas rutrum, nisi sed consequat porttitor, nulla nibh volutpat velit, ac volutpat orci augue quis elit. Nullam velit urna, elementum et felis eget, tempor fermentum magna. Sed id ipsum egestas, eleifend turpis eu, fermentum tellus. Nunc id mi in justo posuere rutrum at quis diam. Mauris vulputate felis tristique mi dapibus, vel semper ipsum iaculis. Suspendisse massa felis, vulputate eget euismod quis, tristique a felis. Proin tempus purus arcu, ut iaculis turpis vehicula a. Nulla facilisi. Sed lac Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas rutrum, nisi sed consequat porttitor, nulla nibh volutpat velit, ac volutpat orci augue quis elit. Nullam velit urna, elementum et felis eget, tempor fermentum magna. Sed id ipsum egestas, eleifend turpis eu, fermentum tellus. Nunc id mi in justo posuere rutrum at quis diam. Mauris vulputate felis tristique mi dapibus, vel semper ipsum iaculis. Suspendisse massa felis, vulputate eget euismod quis, tristique a felis. Proin tempus purus arcu, ut iaculis turpis vehicula a. Nulla facilisi. Sed lac  Proin tempus purus arcu, ut iaculis turpis vehicula a. Nulla facilisi. Sed la",
                id: "103",
                date: "04/15/2022",
                source: "How to Read a Book",
                type: { id: 1, name: 'Ideas' },
            },
        ],

        //tags
        tagsData: {
            type: 0,
            size: 10,//显示多少条数据
            totalEntires: 0,
            totalTags: 0,
            tagsList: [
                {
                    id: 4,
                    name: "Top_Tags",//名称
                    title: "Top Tags",//标题
                    default: "Top tags will be displayed here after they’ve been created.",//空值描述
                    showAll: 0,//是否可以显示全部
                    isShowTab: false,
                    needShow: true,//是否需要显示
                    btnName: "Show all",
                    list: []//列表
                },
                {
                    id: 1,
                    name: "Web_Sources",
                    title: "Top Web Sources",
                    default: "Most-sourced websites will be shown here after they’ve been created.",
                    showAll: 0,
                    isShowTab: false,
                    needShow: true,
                    btnName: "Show all",
                    list: []
                },
                {
                    id: 3,
                    name: "My_Books",
                    title: "Top Books",
                    default: "Most-sourced books will be shown here after they’ve been created.",
                    showAll: 0,
                    isShowTab: false,
                    needShow: true,
                    btnName: "Show all",
                    list: []
                }
            ],
        },

        //选项卡信息
        tabInfo: {
            currentTab: 0, //当前选项卡
            tabList: [
                //选项卡:0全部 1.网页 2.脑洞 3.书籍
                {
                    name: "All",
                    id: 0,
                },
                {
                    name: "Web",
                    id: 1,
                },
                {
                    name: "Book",
                    id: 3,
                },
                {
                    name: "Ideas",
                    id: 2
                },
            ],
        },

        //搜索框
        searchData: {
            placeholder: "Search all…",
            searchValue: "",
            tagValue: "",
            sourceValue: "",
            name: "uncheck",
            list: [],
            tip: "",
        },
        //分页
        pagination: {
            layout: "prev, pager, next",
            show: 0,
            to: 0,
            pageSize: 6,
            page: 1,
            total: 0,
        },
        isLoading: false,
    };
};

const state = initState();

const mutations = {

    setEntries(state, data) {
        state.cardList = data.content;
        state.pagination.total = data.totalElements;
        if (state.pagination.page !== 1) {
            state.pagination.show = (state.pagination.page - 1) * state.pagination.pageSize + 1;
        } else {
            state.pagination.show = 1
        }

        state.pagination.to = state.pagination.page * state.pagination.pageSize
        if (state.pagination.to > data.totalElements) {
            state.pagination.to = data.totalElements
        }
        // console.log(data, 'res');
    },
    setLeftEntries(state, data) {
        // console.log(data, 'data');
        state.tagsData.totalEntires = data.count.Total_Entries
        state.tagsData.totalTags = data.count.Total_Tags
        state.tagsData.tagsList.map(item => {
            if (item.id == 3) {
                item.list = data.My_Books.Books;
                item.list.map((element, location) => {
                    element.id = 'My_Books_' + location;
                    element.type = 'books'
                })
                item.showAll = data.My_Books.show_all
            } else if (item.id == 1) {
                item.list = data.Web_Sources.Sources
                item.list.map((element, location) => {
                    element.id = 'Web_Sources_' + location;
                    element.type = 'sources'
                })
                item.showAll = data.Web_Sources.show_all
            } else if (item.id == 4) {
                item.list = data.Top_Tags.Tags
                item.list.map((element, location) => {
                    element.id = 'Top_Tags_' + location;
                    element.type = 'tags'
                })
                item.showAll = data.Top_Tags.show_all
            }
        })
        if (state.tabInfo.currentTab == 1) {//只显示网页
            state.tagsData.tagsList.map(item => {
                item.needShow = true;
                if (item.id !== 1) {
                    item.needShow = false;
                    item.list = []
                }
            })
        } else if (state.tabInfo.currentTab == 3) {//只显示书籍
            state.tagsData.tagsList.map(item => {
                item.needShow = true;
                if (item.id !== 3) {
                    item.needShow = false;
                    item.list = []
                }
            })
        } else if (state.tabInfo.currentTab == 2) {//只显示标签
            state.tagsData.tagsList.map(item => {
                item.needShow = true;
                if (item.id !== 4) {
                    item.needShow = false;
                    item.list = []
                }
            })
        } else {
            state.tagsData.tagsList.map(item => {
                item.needShow = true;
            })
        }
    },
    resetState(state) {
        Object.assign(state, initState());
    }
};

const actions = {
    //获取卡片
    async getEntries() {
        state.isLoading = true;
        let res = await api.home.getEntriesApi({
            userId: this.state.common.userInfo.userId,//用户id
            status: 1,//帖子状态 1正常，2删除
            type: state.tabInfo.currentTab,//0全部 1.网页 2.脑洞 3.书籍
            page: state.pagination.page,//页码
            size: state.pagination.pageSize,//条数
            fuzzyVal: state.searchData.searchValue,//模糊查询,
            label: state.searchData.tagValue,//标签,
            source: state.searchData.sourceValue,//来源
            deleteDate: "",
        })

        if (isCode(res)) {
            setTimeout(() => {
                state.isLoading = false;
            }, 1000);
            this.commit('home/setEntries', res.data);
        }
    },
    //获取首页左侧tags数据
    async getLeftEntries() {
        let res = await api.home.getLeftEntriesApi({
            userId: this.state.common.userInfo.userId,//用户id
            type: state.tagsData.type,//0全部，1：网页，3：书籍，4标签
            size: state.tagsData.size,//显示条数
        })
        if (isCode(res)) {
            this.commit('home/setLeftEntries', res.data);
            return res
        }
    },
};

const getters = {};

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
};